﻿/***************************************************************************************
   Date    : February 7, 2007
   Purpose : Implement the Scoreboard Class           
   Maintenance Log : 
   Revision		Date			Description 
   --------		---------		-----------------
	1.0		    7/02/2007		Initial Release

****************************************************************************************/

using System;

namespace Hclt.Msft.Til.CricketScoreboard
{
    /// <summary>
    /// Populates The Different Data Associated With Batsman, Bowler, Extras, Team Total Score,
    /// Team Total Balls, Batting Team Name, Bowling Team Name
    /// </summary>    
    public class Scoreboard
    {

        #region Private Instance Variables

        private BatsmanInfoCollection batsmanInfoCollectionObj;
        private BowlerInfoCollection bowlerInfoCollectionObj;
        private ExtrasInfo extrasInfoObj;
        private int teamTotalScore;
        private int teamTotalBalls;
        private bool inningsOver;
        private string matchSummary;
        private string battingTeamName;
        private string fieldingTeamName;

        #endregion

        #region Public Properties

        /// <summary>
        /// Sets Or Gets The BatsmanInfoCollection Object
        /// </summary>
        public BatsmanInfoCollection BatsmanDetails
        {
            get
            {
                return batsmanInfoCollectionObj;
            }
            set
            {
                batsmanInfoCollectionObj = value;
            }
        }

        /// <summary>
        /// Sets Or Gets The BowlerInfoCollection Object
        /// </summary>
        public BowlerInfoCollection BowlerDetails
        {
            get
            {
                return bowlerInfoCollectionObj;
            }
            set
            {
                bowlerInfoCollectionObj = value;
            }
        }

        /// <summary>
        /// Sets Or Gets The ExtrasInfo Object
        /// </summary>
        public ExtrasInfo ExtrasDetails
        {
            get
            {
                return extrasInfoObj;
            }
            set
            {
                extrasInfoObj = value;
            }
        }

        /// <summary>
        /// Sets Or Gets The Total Score Of The Batting Team.
        /// </summary>
        public int TeamTotalScore
        {
            get
            {
                return teamTotalScore;
            }
            set
            {
                teamTotalScore = value;
            }            
        }

        /// <summary>
        /// Sets Or Gets The Total Balls Played By The Batting Team.
        /// </summary>
        public int TeamTotalBalls
        {
            get
            {
                return teamTotalBalls;
            }
            set
            {
                teamTotalBalls = value;
            }
        }

        /// <summary>
        /// Gets The Total Wickets Down Of The Batting Team.
        /// </summary>
        public int TotalWicketsDown
        {
            get
            {
                int totalWicketsDown = 0;
                int noOfBatsman = batsmanInfoCollectionObj.Count;

                for (int counter = 0; counter < noOfBatsman; counter++)
                {
                    if (batsmanInfoCollectionObj[counter].Status == BatsmanStatus.Out)
                    {
                        totalWicketsDown++;
                    }
                }

                return totalWicketsDown;
            }         
        }

        /// <summary>
        /// Gets The Total Extras Of The Batting Team.
        /// </summary>
        public int TotalExtras
        {
            get
            {
                return (extrasInfoObj.TotalByes + extrasInfoObj.TotalNoBalls + extrasInfoObj.TotalWideBalls);
            }            
        }

        /// <summary>
        /// Gets The Total Overs Played By The Batting Team.
        /// </summary>
        public string TotalOvers
        {
            get
            {
                return string.Concat(Convert.ToInt32(teamTotalBalls / 6), "." , (teamTotalBalls % 6));
            }
        }

        /// <summary>
        /// Sets Or Gets The Match Summary Inserted By The Scorer In The Scoresheet.
        /// </summary>
        public string MatchSummary
        {
            get
            {
                return matchSummary;
            }
            set
            {
                matchSummary = value;
            }
        }

        /// <summary>
        /// Sets Or Gets The Batting Team Name From The Scoresheet.
        /// </summary>
        public string BattingTeamName
        {
            get
            {
                return battingTeamName;
            }
            set
            {
                battingTeamName = value;
            }
        }

        /// <summary>
        /// Sets Or Gets The Fielding Team Name From The Scoresheet.
        /// </summary>
        public string FieldingTeamName
        {
            get
            {
                return fieldingTeamName;
            }
            set
            {
                fieldingTeamName = value;
            }
        }

        /// <summary>
        /// Sets Or Gets Whether A Custom Message To Be Display Or Not.
        /// </summary>
        public bool InningsOver
        {
            get
            {
                return inningsOver;
            }
            set
            {
                inningsOver = value;
            }
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Function To Check Whether A Six Hit Occured Or Not.
        /// </summary>
        /// <returns>Boolean Value To Indicate A Six Hit</returns>
        public bool CheckSixHit()
        {
            bool returnValue = false;
            int teamPreviousTotalScore = 0;
            Int32.TryParse(Convert.ToString(Utility.GetFromSession("TeamPreviousScore")), out teamPreviousTotalScore);
            if ((teamTotalScore - teamPreviousTotalScore) == Convert.ToInt32(Boundary.Six))
            {
                returnValue = true;
            }
            return returnValue;
        }

        /// <summary>
        /// Function To Check Whether A Four Hit Occured Or Not.
        /// </summary>
        /// <returns>Boolean Value To Indicate A Four Hit</returns>
        public bool CheckFourHit()
        {
            bool returnValue = false;
            int teamPreviousTotalScore = 0;
            Int32.TryParse(Convert.ToString(Utility.GetFromSession("TeamPreviousScore")), out teamPreviousTotalScore);
            if ((teamTotalScore - teamPreviousTotalScore) == Convert.ToInt32(Boundary.Four))
            {
                returnValue = true;
            }
            return returnValue;
        }

        /// <summary>
        /// Function To Check Whether Wicket Fall Occured Or Not.
        /// </summary>
        /// <returns>Boolean Value To Indicate A Wicket Fall</returns>
        public bool CheckWicketFall()
        {
            bool returnValue = true;
            int teamPreviousWicketsDown = 0;
            Int32.TryParse(Convert.ToString(Utility.GetFromSession("TeamPreviousWickets")), out teamPreviousWicketsDown);
            if (teamPreviousWicketsDown == TotalWicketsDown)
            {

                returnValue = false;
            }
            else
            {
                Utility.AddToSession("TeamPreviousWickets", TotalWicketsDown);
            }

            return returnValue;
        }

        /// <summary>
        /// Function To Populate The Two Currently Playing Batsman.
        /// </summary>
        /// <returns>BatsmanInfo Array Of Length Two</returns>
        public BatsmanInfo[] CurrentBatsman()
        {
            BatsmanInfo[] currentBatsman = new BatsmanInfo[2];
            int index = 0;
            int noOfBatsman = batsmanInfoCollectionObj.Count;
            
            for (int counter = 0; (counter < noOfBatsman) && (index < 2) ; counter++)
            {
                if (batsmanInfoCollectionObj[counter].Status == BatsmanStatus.Playing)
                {
                    currentBatsman[index++] = batsmanInfoCollectionObj[counter];
                }
            }

            return currentBatsman;
        }

        /// <summary>
        /// Function To Populate The Currently Bowling Bowler.
        /// </summary>
        /// <returns>BowlerInfo Object</returns>
        public BowlerInfo CurrentBowler()
        {
            BowlerInfo currentBowler= null;
            int noOfBowler = bowlerInfoCollectionObj.Count;

            for (int counter = 0; counter < noOfBowler; counter++)
            {
                if (bowlerInfoCollectionObj[counter].Status == BowlerStatus.Bowling)
                {
                    currentBowler = bowlerInfoCollectionObj[counter];
                }
            }

            return currentBowler;
        }

        #endregion
    }
}
